package com.android.cattlefarm;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.cattlefarm.util.FileEntity;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Huang on 2015/7/4.
 */
public class FileAdapter extends BaseAdapter {
    private Context context;
    private List<FileEntity> dataList;
    private boolean selected ;

    public FileAdapter(Context context, List<FileEntity> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.file_item, parent, false);
            convertView.setTag(new FileHolder(convertView));
        }
        FileHolder holder = (FileHolder)convertView.getTag();
        FileEntity  item = dataList.get(position);
        holder.checkBox.setChecked(false);
        holder.checkBox.setVisibility(View.GONE);
        if(item.isDirectory()){
            holder.icon.setVisibility(View.VISIBLE);
            holder.count.setVisibility(View.VISIBLE);
            holder.count.setText("共" + item.getFileCount() + "项");

        } else {
            if(selected) {
                holder.checkBox.setVisibility(View.VISIBLE);
                holder.checkBox.setChecked(item.isSelected());
            } else {
                holder.checkBox.setVisibility(View.GONE);
            }
            holder.icon.setVisibility(View.GONE);
            holder.count.setVisibility(View.GONE);
        }
        holder.name.setText(item.getName());
        holder.time.setText(getStringDate(item.getModifyTime()));
        return convertView;
    }
    /**
     * 获取现在时间
     *
     * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
     */
    public String getStringDate(long time) {
        Date currentTime = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }
    class FileHolder{
        ImageView icon ;
        TextView name,count, time;
        CheckBox checkBox;

        public FileHolder(View v) {
            icon = (ImageView) v.findViewById(R.id.file_item_directory_icon);
            name = (TextView) v.findViewById(R.id.file_item_name);
            count = (TextView) v.findViewById(R.id.file_item_directory_count);
            time = (TextView) v.findViewById(R.id.file_item_time);
            checkBox = (CheckBox) v.findViewById(R.id.file_item_check);
        }
    }
}
